class PushSumNode extends Node{
   var s = self.path.name.toDouble
   var w = 1.0
   var count = 0
   var prevRatio = 0.0
   var gotMsg = false
   
	def receive = {
	  case SetNeighbours(n:Int, t:Int) =>
      {
          if (t == Networks.LineNetwork)
          {
            this.loadLineNeighbours(n)
          }
          else if (t == Networks.GridNetwork)
          {
            this.loadGridNeighbours(n)
          }
          else if (t == Networks.ImperfectGridNetwork)
          {
            this.loadImperfectGridNeighbours(n)
          }
          
          sender ! LoadedNeighbours
          //neighbours.foreach(x => print(x+","))
    	  //println("Set up done for: " + self.path.name)
    	  
      }
      case Pair(s: Double, w: Double) => 
      {
        if (!gotMsg)
        {
          Main.NoOfActorsThatGotMessage += 1
          gotMsg = true
          //println(self.path.name)
        }
        
        var sendMsg = true
        count += 1
    	if (count == 3)
        {
    	  val r = ((this.s + s) / (this.w + w))
          val delta = Math.abs(prevRatio - r)
          //if (delta < 0.00001)
          //{
          //	  println(self.path.name + " delta: " + delta + " r: " + r)
          //}
          if (delta > 0.0000000001)
          {
            count = 0
            prevRatio = ((this.s + s) / (this.w + w))
          }
          else
          {
            println(self.path.name + " delta: " + delta + " ratio: " + r)
            context.system.shutdown
            sendMsg = false
          }
        }
        
        if (sendMsg){
	        this.s = (this.s + s) /2 
	        this.w = (this.w + w) /2
	        
	        var sentTo = 0
	        if (Main.network == Networks.FullNetwork)
	        {
	            val me = self.path.name.toInt
	        	while(sentTo == 0 || sentTo == me)
	        	{
	        	  sentTo = rnd.nextInt(Main.nOfActors+1)
	        	}
	            
	            val actor = context.system.actorSelection("akka://System/user/" + sentTo)
	    		actor ! Pair(this.s, this.w)
	        }
	        else
	        {
	        	sentTo = rnd.nextInt(neighbours.length)
	        	val actor = context.system.actorSelection("akka://System/user/" + neighbours(sentTo))
	        	actor ! Pair(this.s, this.w)
	        }
	    	//println(self.path.name + " is sending to " +  neighbours(sentTo) + " index: " + sentTo + " current len: " + neighbours.length)
        }
      }  
      case Start =>
      {
            var sentTo = 0
    	    if (Main.network == Networks.FullNetwork)
            {
                val me = self.path.name.toInt
            	while(sentTo == 0 || sentTo == me)
            	{
            	  sentTo = rnd.nextInt(Main.nOfActors+1)
            	}
                
                val actor = context.system.actorSelection("akka://System/user/" + sentTo)
			    actor ! Pair(this.s, this.w)
            }
            else
            {
            	sentTo = rnd.nextInt(neighbours.length)
            	val actor = context.system.actorSelection("akka://System/user/" + neighbours(sentTo))
			    actor ! Pair(this.s, this.w)
            }
    	  	//println(self.path.name + " is sending to " +  neighbours(sentTo) + " index: " + sentTo + " current len: " + neighbours.length)
			
      }  
	}
}